﻿using HR.IService;
using HR.Models;

namespace HR.Service
{
    public class SalaryGrantDetailsService : ISalaryGrantDetailsService
    {
        private readonly HRDBContext db;

        public SalaryGrantDetailsService(HRDBContext db)
        {
            this.db = db;
        }
        public Task<List<salary_grant_detail>> SalaryGrantDetailByIdFalse(string salary_grant_id)
        {
            return Task.Run(() =>
            {
                //先拿到详情集合的数据
                List<salary_grant_detail> sgds = db.salary_grant_details.Where(sgd => sgd.salary_grant_id == salary_grant_id).ToList();
                List<salary_grant_detail> list = new List<salary_grant_detail>();
                //然后遍历查询是判断否有发过工资
                foreach (salary_grant_detail item in sgds)
                {
                    //在人力资源档案数据中筛选匹配的职工并判断发放过工资的
                    if (db.human_files.Where(hf => hf.human_id == item.human_id && hf.salary_grant_status == false).ToList().Count > 0)
                    {
                        list.Add(item);
                    }
                }
                return list;
            });
        }
        public Task<List<salary_grant_detail>> SalaryGrantDetailById(string salary_grant_id)
        {
            return Task.Run(() =>
            {
                //先拿到详情集合的数据
                List<salary_grant_detail> sgds = db.salary_grant_details.Where(sgd => sgd.salary_grant_id == salary_grant_id).ToList();
                List<salary_grant_detail> list = new List<salary_grant_detail>();
                //然后遍历查询是判断否有发过工资
                foreach (salary_grant_detail item in sgds)
                {
                    //在人力资源档案数据中筛选匹配的职工并判断发放过工资的
                    if (db.human_files.Where(hf => hf.human_id == item.human_id && hf.salary_grant_status == true).ToList().Count > 0)
                    {
                        list.Add(item);
                    }
                }
                return list;
            });
        }

        public Task<int> SalaryGrantDetailUpdate(List<salary_grant_detail> sgds)
        {
            return Task.Run(() =>
            {
                decimal salary_paid_sum = 0;
                foreach (salary_grant_detail sgd in sgds)
                {
                    salary_grant_detail? gd = db.salary_grant_details.Find(sgd.grd_id);
                    gd.bouns_sum = sgd.bouns_sum;
                    gd.sale_sum = sgd.sale_sum;
                    gd.deduct_sum = sgd.deduct_sum;
                    gd.salary_paid_sum = sgd.salary_paid_sum;
                    //修改人力资源档案的发放工资状态为已发放  True，这里还未复核，在复核过后再修改发放状态
                    //human_file? hf = db.human_files.Where(hm => hm.human_id == sgd.human_id).FirstOrDefault();
                    //hf.salary_grant_status = true;
                    //累加总金额，到简略表
                    salary_paid_sum += (decimal)sgd.salary_paid_sum;
                }
                //修改简略表中的总金额
                salary_grant? sgt = db.salary_grants.Where(sg => sg.salary_grant_id == sgds[0].salary_grant_id).FirstOrDefault();
                sgt.salary_paid_sum = salary_paid_sum;
                sgt.check_status = 0;
                return db.SaveChanges();
            });
        }

    }
}
